// 接口

import { type } from "os";

{}
// 1.接口定义
interface ArticleInterface <B, C>{
  author: string,
  title: string,
  isLocked: B,
  comments: C[],
};

type comment = {
  time: Date,
  person: string,
  content: string,
}

let hd: ArticleInterface<boolean, comment> = {
  author: '小明',
  title: '小明的文章',
  isLocked: true,
  comments: [{
    time: new Date(),
    person: '小红',
    content: '写的不错'
  }],
}

// 2.接口拓展值与接口继承
enum Sex {
  gril,
  boy,
}
interface Person {
  name: string,
  age: number,
  sex?: Sex,
  [key: string]: any,
  move(): void;
};

let people1: Person = {
  name: 'xiaohong',
  age: 1,
  sex: Sex.boy,
  color: 'red',
  move() {
    console.log(this.name+'----move');
  }
}
people1.move();

// 接口继承
interface childInterface extends Person{
  hobby: string[],
  setHobby(...item: string[]): string[];
};
let person2: childInterface ={
  name: 'xiaohong2',
  age: 1,
  sex: Sex.boy,
  hobby: [],
  move() {
    console.log(this.name+'----move');
  },
  setHobby(...item: string[]): string[] {
    this.hobby.push(...item);
    return this.hobby;
  }
};

console.log(person2.setHobby('玩电脑', '1', 'abc'));

// 3.模拟接口实现在线支付
interface Pay {
  pay(price: number): void;
}; 

class WxPay implements Pay{
  public pay(price: number): void {
    console.log('微信支付' + price + '元');
  }
};

class Alipay implements Pay {
  public pay(price: number): void {
    console.log('支付宝付款' + price + '元');
  }
};

function payfn(type: string): WxPay | Alipay {
  let pay: WxPay | Alipay = null;
  switch (type) {
    case '微信':
      pay = new WxPay();
      break;
    case '支付宝':
      pay = new Alipay();
      break;
    default:
      console.log('类型错误');
      break;
  }
  return pay;
};

payfn('微信').pay(300);
payfn('支付宝').pay(800);

// 4.type和interface的区别
// type可以命名类型的别名和联合类型
type t1 = boolean;
type t2 = 'body' | 'girl';
type per1 = {
  isTrue: t1,
  sex: t2,
  [key: string]: any
};

let p1: per1 = {
  isTrue: false,
  sex: 'body',
  other: 1
};

// 接口继承的区别
// interface方式
interface p1 {
  get(name: string): string,
  age: number
}

interface p2 extends p1{
  sex: 1,
}

// type方式
type t3 = {
  get(name: string): string,
  age: number
};
type t4 = {
  sex: 1,
};

type t5 = t3 & t4;
type t6 = t3 | t4;

let p3: t5 = {
  get(name: string): string {
    return 'name';
  },
  age: 1,
  sex: 1,
}

let p4: t6 = {
  // get(name: string): string {
  //   return 'name';
  // },
  // age: 1,
  sex: 1,
}

// type 和  interface的混合使用
type t7 = {
  color: null,
};

interface t8 extends t7 {
  like: string
}

let per8: t8 = {
  color: null,
  like: 'red'
};
console.log('------------' + JSON.stringify(per8));